//
//  LTTitleView.m
//  LocalTravel
//
//  Created by zheng ji on 14-3-6.
//  Copyright (c) 2014年 jxw. All rights reserved.
//

#import "LTTitleView.h"
#import "constants.h"

@interface LTTitleView()
{
    CGRect _frame;
    UILabel* _lblTitle;
    UILabel* _lblDistance;
    
    UIPopoverController* _popoverDistince;
    UIPopoverController* _popoverCity;
    UILabel *_loadMoreTip;//加载更多的提示
    UIActivityIndicatorView *_activityIndicator;//加载状态
}
@end
@implementation LTTitleView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        _frame=frame;
        [self initUI];
    }
    return self;
}
-(void) setCurrentCity:(NSString*)city{
    _lblTitle.text=[NSString stringWithFormat:@"切换城市：%@ ▽",city];
    
}
//将距离设置为不限
-(void) clearDistance{
    [self setDistance:0 text:@"不限"];
}
-(void) setDistance:(int)distance text:(NSString*)text{
    _lblDistance.text=[NSString stringWithFormat:@"距离：%@ ▽",text];
}

-(void)addActivityIndicator{
    _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    
    [_activityIndicator stopAnimating];
    _activityIndicator.hidden=YES;
    
    CGRect frame=_activityIndicator.frame;
    frame.origin.x=floor(self.frame.size.width/2-100);
    frame.origin.y=0;
    _activityIndicator.frame= frame;
    
    [self addSubview:_activityIndicator];
    
    _loadMoreTip=[[UILabel alloc] initWithFrame:CGRectMake(frame.origin.x +25 ,0, 200, 20)];
    _loadMoreTip.textAlignment=NSTextAlignmentLeft;
    _loadMoreTip.text=@"";
    _loadMoreTip.textColor=[UIColor redColor];
    //_loadMoreTip.textColor=[UIColor blueColor];
    _loadMoreTip.font=[UIFont systemFontOfSize:16];
    _loadMoreTip.backgroundColor=[UIColor clearColor];
    [self addSubview:_loadMoreTip];
    //_loadMoreTip.hidden=YES;
    
    
    //_loadMoreTip.hidden=NO;
}
-(void) showActivityIndicator:(NSString*)tipText{
    [_activityIndicator startAnimating];
    _activityIndicator.hidden=NO;
    _loadMoreTip.text=tipText;
    _loadMoreTip.hidden=NO;
}
-(void) stopActivityIndicator:(NSString*)tipText{
    [_activityIndicator stopAnimating];
    _activityIndicator.hidden=YES;
    _loadMoreTip.text=tipText;
    _loadMoreTip.hidden=NO;
}
-(void)initUI{
 

    
    //self.backgroundColor=PANEL_BACKGROUND_COLOR;
    _lblTitle=[[UILabel alloc] initWithFrame:CGRectMake(15, 0, 200, 20)];
    _lblTitle.text=@"切换城市：未检查到";
    _lblTitle.textColor=[UIColor blueColor];
    _lblTitle.autoresizingMask=UIViewAutoresizingFlexibleWidth;
    _lblTitle.backgroundColor=[UIColor clearColor];
    _lblTitle.userInteractionEnabled=YES;
    
    
    UITapGestureRecognizer* tapCity=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cityChange:)];
    tapCity.numberOfTapsRequired=1;
    tapCity.numberOfTouchesRequired=1;
    
    [_lblTitle addGestureRecognizer:tapCity];

    
    [self addSubview:_lblTitle];
     tapCity=nil;
    
    
    _lblDistance=[[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width-200, 0, 200, 20)];
    _lblDistance.text=@"距离：不限 ▽";
    _lblDistance.textColor=[UIColor blueColor];
    _lblDistance.autoresizingMask=UIViewAutoresizingFlexibleWidth;
    _lblDistance.backgroundColor=[UIColor clearColor];
    _lblDistance.userInteractionEnabled=YES;
    UITapGestureRecognizer* tapDistance=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(distanceChange:)];
    tapDistance.numberOfTapsRequired=1;
    tapDistance.numberOfTouchesRequired=1;
    
    [_lblDistance addGestureRecognizer:tapDistance];
    
    [self addSubview:_lblDistance];
    
    tapDistance=nil;

    [self addActivityIndicator];
    
}
-(void) cityChange:(UITapGestureRecognizer*)sender{
    
    //if(_popoverCity==nil){
       // UIStoryboard* storyBoard=[UIStoryboard storyboardWithName:@"second" bundle:nil];
        LTCityViewController* cityController=[[LTCityViewController alloc]init];
        cityController.delegate=self;
        UINavigationController* naviController=[[UINavigationController alloc]initWithRootViewController:cityController];
        
        //UINavigationController* vc=(UINavigationController*)[storyBoard instantiateViewControllerWithIdentifier:@"CityNavigationController"];
       // vc.delegate=self;
        _popoverCity=[[UIPopoverController alloc]initWithContentViewController:naviController];
        _popoverCity.popoverContentSize=CGSizeMake(260, 500);
        
        cityController=nil;
        
    //}
    //CGRect rect= _lblTitle.frame;
    [_popoverCity presentPopoverFromRect:_lblTitle.frame inView:self permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    
    
}


-(void) distanceChange:(UITapGestureRecognizer*)sender{
    
    if(![self.delegate hasCurrentLocation]){
        UIAlertView* alertView=[[UIAlertView alloc]initWithTitle:@"提示" message:@"没有获取到您当前的位置!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
        alertView=nil;
        return ;
    }
    if(_popoverDistince==nil){
        UIStoryboard* storyBoard=[UIStoryboard storyboardWithName:@"second" bundle:nil];
        UITableViewController* vc=(UITableViewController*)[storyBoard instantiateViewControllerWithIdentifier:@"LTDistanceViewController"];
        vc.tableView.delegate=self;
        _popoverDistince=[[UIPopoverController alloc]initWithContentViewController:vc];
        _popoverDistince.popoverContentSize=CGSizeMake(150, 300);
        
       
        
    }
    CGRect rect= _lblDistance.frame;
    [_popoverDistince presentPopoverFromRect:rect inView:self permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(self.delegate !=nil){
     
     
       if(indexPath.row==0){
           [self.delegate distanceSelected:3000];
           _lblDistance.text=@"距离：3公里以内 ▽";
       }else if(indexPath.row==1){
           [self.delegate distanceSelected:5000];
           _lblDistance.text=@"距离：5公里以内 ▽";
       }else if(indexPath.row==2){
           [self.delegate distanceSelected:10000];
           _lblDistance.text=@"距离：10公里以内 ▽";
       }else if(indexPath.row==3){
           [self.delegate distanceSelected:15000];
           _lblDistance.text=@"距离：15公里以内 ▽";
       }else if(indexPath.row==4){
           [self.delegate distanceSelected:20000];
           _lblDistance.text=@"距离：20公里以内 ▽";
       }else if(indexPath.row==5){
           [self.delegate distanceSelected:0];
           _lblDistance.text=@"距离：不限 ▽";
       }
    }
    [_popoverDistince dismissPopoverAnimated:YES];
}
-(void)citySelected:(NSString* )cityID  cityName:(NSString*)cityName{
    [_popoverCity dismissPopoverAnimated:YES];
    [self setCurrentCity:cityName];
    
    if(self.delegate !=nil){
        [self.delegate citySelected:cityID cityName:cityName];
    }
   // NSLog(@"%@ %@",cityID,cityName);
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
